package top.huxy.boot.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.huxy.boot.exception.RequestAbortedException;
import top.huxy.boot.exception.RequestParamIllegalException;
import top.huxy.boot.mapper.ArticleMapper;
import top.huxy.boot.model.dto.DTO_ADDArticle;
import top.huxy.boot.model.dto.DTO_UpdateArticle;
import top.huxy.boot.model.dto.Pager;
import top.huxy.boot.model.vo.user.VO_Article;
import top.huxy.boot.service.ArticleService;
import top.huxy.boot.service.TagService;
import top.huxy.boot.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author HuXY
 * @since 2021-12-21 09:59:01
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagService tagService;

    @Override
    public List<VO_Article> getArticleList(Pager pager, String type) {
        try {
            PageHelper.startPage(pager.getPage(), pager.getPageSize(), "create_time desc");
            return articleMapper.getArticleList(type);
        } finally {
            PageHelper.clearPage();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addArticle(DTO_ADDArticle article) {
        //插入文章
        long now = System.currentTimeMillis();
        article.setCreateTime(now);
        int res = articleMapper.addArticle(article);
        if (res == 0) {
            throw new RequestAbortedException("新增文章失败");
        }
        if (res != 1) {
            throw new RequestAbortedException("新增文章失败,不是预期的值");
        }

        Integer blogId = articleMapper.getArticleIdByCreateTime(now);
        //保存文章标签
        String tags = article.getTags();
        if (!StringUtils.isEmpty(tags)) {
            String[] split = tags.split(",");
            for (String tag : split) {
                try {
                    //保存文章标签
                    tagService.addStringTag(tag);
                } catch (Exception ignored) {
                }
                //应用文章标签
                int tagId = tagService.queryTagId(tag);
                // TODO: 2021/12/21 dao修改的表的索引有问题 
                tagService.applyTag(blogId, tagId);
            }
        }
        return blogId;
    }

    @Override
    public VO_Article getArticleById(Integer id) {
        return articleMapper.getArticleById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArticle(DTO_UpdateArticle article) {
        if (StringUtils.isNaN(article.getId())) {
            throw new RequestParamIllegalException("id is NaN");
        }

        //更新文章
        boolean update1 = articleMapper.updateArticle(article) == 1;

        //更新标签
        String tags = article.getTags();
        if (StringUtils.isEmpty(tags)) {
            tags = "";
        }
        tagService.changeArticleTag(article.getId(), Arrays.asList(tags.split(",")));
    }
}

